package com.jushu.storbox.adapter;
import java.lang.ref.SoftReference;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.jushu.storbox.util.PublicTools;
import com.jushu.storbox.util.image.BitmapUtil;

public class LookImageAdapter extends PagerAdapter {
	private List<String> mPaths;
	private Context context;
	private Handler handler;
	private List<SoftReference<Bitmap>> browserBitmaps;
	public LookImageAdapter(Context context,List<String> paths, Handler handler, List<SoftReference<Bitmap>> browserBitmaps) {
		this.context = context;
		this.mPaths = paths;
		this.handler = handler;
		this.browserBitmaps = browserBitmaps;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mPaths.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object obj) {
		// TODO Auto-generated method stub
		return view == (View) obj;
	}

	@Override
	public Object instantiateItem (ViewGroup container, int position) {
		ImageView iv = new ImageView(context);
		try {
			Bitmap bm = BitmapUtil.createImageThumbnail(mPaths.get(position), PublicTools.getWindowWidth(context), PublicTools.getWindowHeight(context));
			if (null != bm) {
				browserBitmaps.add(new SoftReference<Bitmap>(bm));
				iv.setImageBitmap(bm);
				iv.setOnClickListener(new ViewOnClickListener(handler));
			} else {
//				iv.setImageResource(R.drawable.);
			}
		} catch (OutOfMemoryError e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		((ViewPager)container).addView(iv, 0);
		return iv;
	}
	
	@Override
	public void destroyItem (ViewGroup container, int position, Object object) {
		container.removeView((View)object);
	}
	private class ViewOnClickListener implements OnClickListener 
	{
		private Handler handler = null;
		public ViewOnClickListener(Handler handler)
		{
			this.handler = handler;
		}
		@Override
		public void onClick(View v)
		{
			Message message = new Message();
			message.what = 1;
			handler.sendMessage(message);
		}
	}
}

